package com.example.logisticsingle.login;



import com.example.logisticsingle.String.StringUtils;
import com.example.logisticsingle.constants.Config;
import com.example.logisticsingle.utils.CookieUtil;
import com.example.logisticsingle.login.SSOSessionIdHelper;
import com.example.logisticsingle.entity.SSOUser;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 单点登录登录铺助类

 *
 */
public class SSOTokenLoginHelper {
    /**
     * logout 操作，通过request请求
     * @param request
     */
    public static void logout(HttpServletRequest request) {
        String headerSessionId = request.getHeader(Config.SSO_SESSIONID);
        logout(headerSessionId);
    }
    /**
     *	 登出操作
     * @param token
     */
    public static void logout(String token) {
        if (StringUtils.isNotNull(token))
           SSOLoginStoreHelper.remove(token);
    }
    /**
     * 	根据Token获取用户信息
     * @param token
     * @return
     */
    public static SSOUser loginCheck(String token) {
        if(StringUtils.isNull(token)) return null;
        SSOUser user = new SSOUser();
        user.setUsername("如东");
        return user;
    }
    /**
     * 根据请求获取用户信息
     * @param request
     * @return
     */
    public static SSOUser loginCheck(HttpServletRequest request) {
        String headerSessionId = request.getHeader(Config.SSO_SESSIONID);
        return loginCheck(headerSessionId);
    }

    /**
     * login check
     *
     * @param request
     * @param response
     * @return
     */
    public static SSOUser loginCheck(HttpServletRequest request, HttpServletResponse response){
        String token = "";
        if(request.getHeader("User-Agent").startsWith("Mozilla")) {
            token = CookieUtil.getValue(request, Config.SSO_SESSIONID);
        }else {
            token = request.getHeader("Authorization");
        }


        // cookie user
        SSOUser ssoUser = SSOTokenLoginHelper.loginCheck(token);
        if (ssoUser != null) {
            return ssoUser;
        }
        // redirect user

        // remove old cookie
        CookieUtil.remove(request, response, Config.SSO_SESSIONID);

        // set new cookie
        String paramSessionId = request.getParameter(Config.SSO_SESSIONID);
        ssoUser = SSOTokenLoginHelper.loginCheck(paramSessionId);
        if (ssoUser != null) {
            CookieUtil.set(response, Config.SSO_SESSIONID, paramSessionId, false);    // expire when browser close （client cookie）
            return ssoUser;
        }

        return null;
    }

    /**
     * get sessionid by cookie
     *
     * @param request
     * @return
     */
    public static String getSessionIdByCookie(HttpServletRequest request) {
        String cookieSessionId = CookieUtil.getValue(request, Config.SSO_SESSIONID);
        return cookieSessionId;
    }

    /**
     * client logout
     *
     * @param request
     * @param response
     */
    public static void logout(HttpServletRequest request,
                              HttpServletResponse response) {

        String cookieSessionId = CookieUtil.getValue(request, Config.SSO_SESSIONID);
        if (cookieSessionId==null) {
            return;
        }

        String storeKey = SSOSessionIdHelper.parseStoreKey(cookieSessionId);
        if (storeKey != null) {
            SSOLoginStoreHelper.remove(storeKey);
        }

        CookieUtil.remove(request, response, Config.SSO_SESSIONID);
    }

}
